﻿CREATE PROCEDURE [dbo].[SupplierReturnLinesDelete]
(
	@Original_Id int,
	@Original_MD datetime
)
AS
DECLARE @strMsg nvarchar(500), @intErrCode int, @intRet int, @orgDocTypeId int

SET NOCOUNT ON
IF (@Original_Id Is Null) OR (@Original_MD Is Null) 
BEGIN
	SELECT @intErrCode = 1, 
	@strMsg = 'Неправильные параметры в вызове процедуры ' + Object_Name(@@Procid) + ' !'
	RAISERROR (@strMsg, 11, @intErrCode)
	RETURN @intErrCode
END

SELECT @orgDocTypeId = d.DocTypeId 
--FROM CA.Documents WHERE Id = @Original_Id AND MD = @Original_MD
FROM CA.GoodsDocLines l INNER JOIN CA.Documents d on d.Id = l.DocId 
WHERE l.Id = @Original_Id AND l.MD = @Original_MD
IF (@@rowcount <> 1)
BEGIN
	SELECT @intErrCode = 2, 
	@strMsg = 'Данные изменены после последнего запроса!'
	RAISERROR (@strMsg, 11, 1)
	RETURN @intErrCode
END

	SET NOCOUNT OFF;
DELETE FROM [CA].[GoodsDocLines] WHERE Id = @Original_Id AND MD = @Original_MD

RETURN 0